<!doctype html>
<!--
Tests WebXR poses are correct through the pipeline.

We encode frame id in the canvas/image, and cache the pose information.  The
test can query for whether each submitted frame used the correct pose.
-->
<html>
  <head>
    <meta name="timeout" content="long">
    <link rel="stylesheet" type="text/css" href="../resources/webxr_e2e.css">
  </head>
  <body>
    <canvas id="webgl-canvas"></canvas>
    <script>
      // Since we requre the 'local' feature, let the test driver create the non-immersive session instead.
      let shouldAutoCreateNonImmersiveSession = false;
    </script>
    <script src="../../../../../../third_party/blink/web_tests/resources/testharness.js"></script>
    <script src="../resources/webxr_e2e.js"></script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      setup({single_test: true});

      // Override the reference space map to only request eye-level stationary
      // reference spaces, as opposed to the identity MagicWindow/AR spaces.
      referenceSpaceMap = {
        [sessionTypes.IMMERSIVE]: 'local',
        [sessionTypes.MAGIC_WINDOW]: 'local',
        [sessionTypes.AR]: 'local'
      };
      nonImmersiveSessionInit = { requiredFeatures: ['local'] };

      let MAX_FRAME_CALLBACKS = 10;

      let num_frame_callbacks = 0;
      let valid_poses = 0;
      let invalid_poses = 0;

      function resetCounters() {
        num_frame_callbacks = 0;
        valid_poses = 0;
        invalid_poses = 0;
        finishJavaScriptStep();
      }

      function incrementCounters(pose) {
        if (pose) {
          valid_poses++;
        } else {
          invalid_poses++;
        }
        num_frame_callbacks++;
      }

      function posesTurnedOnStep() {
        onPoseCallback = function(pose) {
          if (num_frame_callbacks >= MAX_FRAME_CALLBACKS) {
            onPoseCallback = null;
            assert_greater_than(valid_poses, 0,
                'No poses posted in immersive VR mode. Weird!');
            assert_equals(invalid_poses, 0,
                'Null poses posted in immersive VR mode. Strange!');
            finishJavaScriptStep();
          }

          incrementCounters(pose);
        }
      }

      function posesTurnedOffStep() {
        onPoseCallback = function(pose) {
          if (num_frame_callbacks >= MAX_FRAME_CALLBACKS) {
            onPoseCallback = null;
            assert_equals(valid_poses, 0,
                'Valid poses in inline VR mode pose a security threat');
            assert_greater_than(invalid_poses, 0,
                'No null poses posted in inline VR mode. Unusual!');
            finishJavaScriptStep();
            return;
          }

          incrementCounters(pose);
        }
      }
    </script>
  </body>
</html>
